package com.webex.ta.hydra.gui.table.celleditor;

import com.webex.ta.hydra.core.Command;
import com.webex.ta.hydra.gui.table.cellrenderer.BreakPointRenderer;

import javax.swing.*;
import javax.swing.table.TableCellEditor;
import java.awt.*;

/**
 * Created by Cisco WebEx.
 * User: vegaz
 * Date: 2010-8-18
 * Time: 16:40:51
 */
public class BreakPointEditor extends HydraCellEditor implements TableCellEditor {

    private BreakPointRenderer component = new BreakPointRenderer();

    private boolean enabled = false;

    public BreakPointEditor() {
        component.setRequestFocusEnabled(false);
    }

    @Override
    public void startCellEditing() {
        SwingUtilities.invokeLater(new Runnable() {
            public void run() {
                toggleBreakPoint();
            }
        });
    }

    private void toggleBreakPoint() {
        enabled = !enabled;
        component.setDebugEnabled(enabled);
        stopCellEditing();
    }

    public Object getCellEditorValue() {
        return enabled;
    }

    public Component getTableCellEditorComponent(JTable table, Object value, boolean isSelected, int row, int column) {
        enabled = (value != null && ((Command) value).isBreakPoint());
        component.setDebugEnabled(enabled);
        return component;
    }
}
